﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using BusinessObject;
using StudentAttendanceChecking.Lib;

namespace StudentAttendanceChecking
{
    public partial class frmListStudent : Form
    {
        StudentBO sBO = new StudentBO();
        Account a;
        public static string id;
        private int sortCol = -1;
        private int groupBy = -1;
        public frmListStudent(Account acc)
        {
            InitializeComponent();
            a = acc;
        }

        private void frmListStudent_Load(object sender, EventArgs e)
        {
            //load theo nhom Branch
            groupBy = 1;
            LoadListView(3, false);
        }
        //load listview
        private void LoadListView(int g, bool showGroup)
        {
            //add cac columns cho listview
            listView.ShowGroups = showGroup;
            listView.Columns.Clear();
            listView.Groups.Clear();
            listView.Items.Clear();
            listView.Columns.Add("Name", 200, HorizontalAlignment.Left);
            listView.Columns.Add("Code", 60, HorizontalAlignment.Center);
            listView.Columns.Add("Gender", 60, HorizontalAlignment.Center);
            listView.Columns.Add("Branch", 60, HorizontalAlignment.Center);
            listView.Columns.Add("Class", 60, HorizontalAlignment.Center);
            listView.Columns.Add("Email", 220, HorizontalAlignment.Left);
            listView.Columns.Add("Phone", 90, HorizontalAlignment.Right);
            listView.Columns.Add("Status", 60, HorizontalAlignment.Center);
            //lay du lieu tu dataset
            DataSet ds = sBO.getListStudentByAccount(a);
            DataTable dt = ds.Tables[0];
            bool checkGroup = false;
            ListViewItem lisItem = null;
            foreach (DataRow rows in dt.Rows)
            {
                lisItem = new ListViewItem(rows["Name"].ToString(), 0);
                lisItem.SubItems.Add(rows["StudentID"].ToString());
                bool gender = (bool)rows["Gender"];
                if (gender == true)
                {
                    lisItem.SubItems.Add("Male");
                }
                else
                {
                    lisItem.SubItems.Add("Female");
                }
                lisItem.SubItems.Add(rows["BranchName"].ToString());
                lisItem.SubItems.Add(rows["ClassName"].ToString());
                lisItem.SubItems.Add(rows["Email"].ToString());
                lisItem.SubItems.Add(rows["Phone"].ToString());
                lisItem.SubItems.Add(rows["StudentStatus"].ToString());
                listView.Items.Add(lisItem);
                foreach (ListViewGroup item in listView.Groups)
                {
                    if (item.Header == rows[g].ToString())
                    {
                        checkGroup = true;
                        lisItem.Group = item;
                        break;
                    }
                    else
                    {
                        checkGroup = false;
                    }
                }
                //neu chua co group nay thi moi cho vao day
                if (!checkGroup)
                {
                    ListViewGroup gr = new ListViewGroup(rows[g].ToString());
                    //add group
                    listView.Groups.Add(gr);
                    //gan item theo group
                    lisItem.Group = gr;
                }
            }
            listView.Focus();
        }

        private void listView_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                mnuListView.Show(listView, new Point(e.X, e.Y));
            }
        }

        private void listView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem item = e.Item;
            listView.ShowItemToolTips = true;
            this.Text = item.SubItems[1].Text;
            id = item.SubItems[1].Text;
        }

        private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.Details;
        }

        private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.SmallIcon;
        }

        private void lagreIconToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.LargeIcon;
        }

        private void listToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.List;
        }

        private void titleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView.View = View.Tile;
        }

        private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
        {

            if (e.Column != sortCol)
            {
                //gan gia tri bang cot dang chon
                sortCol = e.Column;
                //sap xep theo asc
                listView.Sorting = SortOrder.Ascending;
            }
            else
            {
                if (listView.Sorting == SortOrder.Ascending)
                {
                    listView.Sorting = SortOrder.Descending;
                }
                else
                {
                    listView.Sorting = SortOrder.Ascending;
                }
            }
            //goi methoad sort cua list
            listView.Sort();
            listView.ListViewItemSorter = new ListViewItemComparer(e.Column, listView.Sorting);
        }

        private void branchNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            groupBy = 2;
            LoadListView(3, true);
        }

        private void classNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            groupBy = 3;
            LoadListView(4, true);
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (groupBy == 1)
            {
                LoadListView(2, false);
            }
            else if (groupBy == 2)
            {
                LoadListView(3, true);
            }
            else if (groupBy == 3)
            {
                LoadListView(4, true);
            }
        }

        private void noneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            groupBy = 1;
            LoadListView(3, false);
        }
    }
}
